package com.camera2.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.camera2.dao.UpdateManager;
import com.camera2.http.GlobalUrl;

import java.io.File;

public class MySetting extends Activity implements View.OnClickListener {

    private final String TAG = "MySetting";


    private TextView tv_version;

    private TextView logout;

    private ProgressBar pb_setting_update;
    private TextView tv_setting_update;
    public static receiveVersionHandler handler;
    private UpdateManager manager = UpdateManager.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_setting);

        tv_version = (TextView) findViewById(R.id.tv_setup_version);
        logout = (TextView) findViewById(R.id.setup_logout);

        findViewById(R.id.setup_update).setOnClickListener(this);
        findViewById(R.id.setup_idea).setOnClickListener(this);
       
        findViewById(R.id.setup_about).setOnClickListener(this);
        findViewById(R.id.setup_logout).setOnClickListener(this);

        pb_setting_update = findViewById(R.id.pb_setting_update);
        tv_setting_update = findViewById(R.id.tv_setting_setup);
        handler = new MySetting.receiveVersionHandler();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setup_update:
                manager.CheckVersion(this, "MySetting");

                break;
            case R.id.setup_idea:
                Toast.makeText(this, "功能开发中", Toast.LENGTH_SHORT).show();
                break;

            case R.id.setup_about:
                Toast.makeText(this, "功能开发中", Toast.LENGTH_SHORT).show();
                break;
            case R.id.setup_logout:
                finish();
                Intent intent = new Intent(this, ActivityLogin.class);
                startActivity(intent);

                break;
        }
    }
    public class receiveVersionHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Toast.makeText(getApplicationContext(), "已是最新版本", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    pb_setting_update.setProgress(msg.arg1);
                    pb_setting_update.setVisibility(View.VISIBLE);
                    tv_setting_update.setVisibility(View.VISIBLE);
                    Log.i(TAG, "handleMessage: " + msg.arg1);
                    if (msg.arg1 == 100) {
                        pb_setting_update.setVisibility(View.GONE);
                        tv_setting_update.setVisibility(View.GONE);
                        Toast.makeText(getApplicationContext(), "下载完成，请到文件管理中安装", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent("android.intent.action.VIEW");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        intent.addCategory("android.intent.category.DEFAULT");
                        String type = "application/vnd.android.package-archive";

                        File outputImage = new File(GlobalUrl.getSavePath());
                        Uri data;
                        if (Build.VERSION.SDK_INT >= 24) {
                            data = FileProvider.getUriForFile(getApplicationContext(), "com.camera2.activity.fileprovider", outputImage);
                        } else {
                            data = Uri.fromFile(outputImage);
                        }
                        intent.setDataAndType(data, type);
                        startActivity(intent);
                    }
                    break;
                case 2:
                    Toast.makeText(getApplicationContext(), "已是最新版本", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
